package com.lanxiao.cloud.modules.oauth.domain.model;

import com.baomidou.mybatisplus.annotation.TableName;
import com.lanxiao.cloud.common.core.error.LoginErrorType;
import com.lanxiao.cloud.common.core.exception.BusinessException;
import com.lanxiao.cloud.common.mybatis.basic.Entity;
import com.lanxiao.cloud.modules.oauth.infrastructure.enums.LoginType;
import java.util.Date;
import org.springframework.util.DigestUtils;

/**
 * @author ：qiaoliang
 * @date ：2021-08-24
 */
@TableName(value = "sys_user_auth",autoResultMap = true)
public class UserAuth extends Entity<Long> {

  /** 用户ID **/
  private Long userId;
  /** 登录类型 **/
  private LoginType type;
  /** 标识 **/
  private String principal;
  /** 密码凭证 **/
  private String credential;

  public UserAuth() {
  }

  public UserAuth(Long userId, LoginType type, String principal, String credential) {
    this.userId = userId;
    this.type = type;
    this.principal = principal;
    this.credential = credential;
  }

  /**
   *  密码加密
   *
   * @author ：qiaoliang

   * @date 2021-09-24 15:56
   */
  public void encryption(Long userId) {
    this.setCredential(DigestUtils.md5DigestAsHex((userId + this.getCredential() + userId).getBytes()));
    this.setUserId(userId);
  }

  /**
   *  密码比对
   *
   * @author ：qiaoliang
   * @param credentials : 请求的密码
   * @date 2021-09-24 15:59
   */
  public void comparison(String credentials) {
    BusinessException.isFalse(this.getCredential()
            .equals(DigestUtils.md5DigestAsHex((this.getUserId() + credentials + this.getUserId()).getBytes())),
        LoginErrorType.PASSWORD_ERROR);
  }

  public Long getUserId() {
    return userId;
  }

  public void setUserId(Long userId) {
    this.userId = userId;
  }

  public LoginType getType() {
    return type;
  }

  public void setType(LoginType type) {
    this.type = type;
  }

  public String getPrincipal() {
    return principal;
  }

  public void setPrincipal(String principal) {
    this.principal = principal;
  }

  public String getCredential() {
    return credential;
  }

  public void setCredential(String credential) {
    this.credential = credential;
  }
}
